import * as actionTypes from './actionTypes'

const defaultState = {
  inputValue: 'wsh3038971',
  list: [
    'Racing car sprays burning fuel into crowd.',
    'Japanese princess to wed commoner.',
    'Australian walks 100km after outback crash.',
    'Man charged over missing wedding girl.',
    'Los Angeles battles huge wildfires.'
  ]
}
// eslint-disable-next-line import/no-anonymous-default-export
export default (state = defaultState, action) => {
  // 可以对state进行深拷贝,最终返回拷贝后的数据
  let newState = JSON.parse(JSON.stringify(state))
  // reducer里只能接收state, 不能改变state
  if (action.type === actionTypes.CHANGE_INPUT) {
    newState.inputValue = action.value
    return newState
  } else if (action.type === actionTypes.CLICK_BTN) {
    newState.list.push(newState.inputValue)
    newState.inputValue = ''
    return newState
  } else if (action.type === actionTypes.ITEM_DELETE) {
    newState.list.splice(action.value, 1)
    return newState
  } else if (action.type === actionTypes.QUWERY_DATA) {
    newState.list = JSON.parse(JSON.stringify(action.value))
    return newState
  }
  return state
}